<!--
Revolver - RSS 

2010-01-28 - Initial working alpha version

Created by Ben Hedrington

-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery.jfeed.pack.js"></script>
<script type="text/javascript">
// Global Variables - When possible pulling form Local Storage set via Options page.
var stopNow = true;
var manifest = new Array();
var curr = -1;
var currTab = 0;
var currUri = "http://rss.cnn.com/rss/cnn_topstories.rss";
if (localStorage["feed"]) { currUri = localStorage["feed"];}
var timeDelay = 10000;
if (localStorage["seconds"]) { timeDelay = (localStorage["seconds"]*1000);}
var maxItems = 10;
if (localStorage["max"]) { maxItems = localStorage["max"];}

// Setup Initial Badge Text
var badgeColor = [139,137,137,137];
chrome.browserAction.setBadgeBackgroundColor({color: badgeColor});
//chrome.browserAction.setBadgeText({text: 'off'});

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
	if (stopNow) {
		go();
		chrome.browserAction.setBadgeText({text: 'on'});
	} else {
		stop();
		chrome.browserAction.setBadgeText({text: ''});
	}
});

// Switch Tab URL functionality.
function swTab(tab, uri) {
	chrome.tabs.update(tab, {url: uri});
}

// Function grabs current tab and kicks off process.
function go() {
	chrome.tabs.getSelected(null, function (tab){
		currTab = tab.id;
	});
	pullReset();	
}

// Function pulls RSS feed, processes and adds to URL manifest for rotating.
function pullReset() {
	stopNow = false;
	curr = -1;
	manifest = [];
	jQuery.getFeed({
	   	url: currUri,
	   	success: function(feed) {
	           	for(var i = 0; i < feed.items.length && i < maxItems; i++) {
		         	var item = feed.items[i];
				manifest[i]=item.link;
	       		}
			moveTab();
			moverInteval = setInterval("moveTab()", timeDelay);
		}    
	});
}

// Stops repetitive process and all processing.
function stop() {
	stopNow = true;	
	clearInterval(moverInteval);
}

// Switches to nex URL in manifest, re-requests feed if at end of manifest.
function moveTab() {
	if (!stopNow){
		if (manifest.length > (curr+1)) {
			curr = curr+1;
			swTab(currTab,manifest[curr])
		} else {
			stop();
			pullReset();
		}
	}
}
</script>
